import {
  comments
} from "../../../../js/modules/mall.js";
import {
  orderlist
} from "../../../../js/modules/order.js"
import {
  storage,
  util,
  dialog,
  api
} from '../../../../js/util';
import {
  router
} from '../../../../js/router';
import authorize from '../../../../js/modules/authorize.js'
import {
  upload
} from '../../../../js/modules/material.js'
const app = getApp()
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"commentList",

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    tablist: ['待评价', '已评价'],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 0,
    lists: [{
      status: 70, //待评价
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "Id",
        sortDirect: "desc"
      }
    }, {
      status: 1, //已评价
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "Id",
        sortDirect: "desc"
      }
    }],
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var that = this;
    this.data.fkId = app.dns.proprietorId;
    this.data.fkFlag = app.dns.proprietor;
    this.loadList();
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          slidewidth: (res.windowWidth / that.data.tablist.length) - 20,
        });
      }
    });
  },
  /**加载商品列表 */
  loadList() {
    var that = this;
    var current = this.data.lists[that.data.activeIndex];
    var param = current.page;
    if (that.data.activeIndex == 1) {
      comments.loadList({state:current.status,fkFlag:this.data.fkFlag}, param, function(content) {
        that.decorateList(that, content, current);
      });
    } else {
      orderlist.loadList(current.status, param, function (content) {
        that.decorateList(that, content, current);
      });
    }
  },
  decorateList(that, content, current) {
    var hasMoreData = content.count > (current.page.PageIndex) * current.page.PageSize;
    var list_key = "lists[" + that.data.activeIndex + "].list";
    //是否有更多数据
    var list_has = "lists[" + that.data.activeIndex + "].hasMoreData";
    //无数据时显示的信息
    var isLoadComplete = "lists[" + that.data.activeIndex + "].isLoadComplete";
    var index = that.data.lists[that.data.activeIndex].list.length;
    var data = content.data;
    //当分页大于1时，累加
    if (current.page.PageIndex > 1) {
      data = that.data.lists[that.data.activeIndex].list.concat(data);
    }
    //如果key为变量作为key值的时候要用[]包裹起来
    that.setData({
      [list_key]: data,
      [list_has]: hasMoreData,
      [isLoadComplete]: true
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      Currency:api.Currency || '￥'
    })
  },
  /**
   * 刷新页面
   */
  refreshPage() {
    this.loadList();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    router.switchTab({
      url: 'center'
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.data.lists[this.data.activeIndex].page.PageIndex = 1;
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function(e) {
    var current = this.data.lists[this.data.activeIndex];
    if (current.hasMoreData) {
      var index = this.data.lists[this.data.activeIndex].page.PageIndex;
      this.data.lists[this.data.activeIndex].page.PageIndex = index + 1;
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 切换当前选择项
   */
  onSwitchItem: function(res) {
    if (this.data.activeIndex == res.detail.currentNum.id) return;
    var activeIndex = res.detail.currentNum.id;
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: activeIndex
    });
    var current = this.data.lists[activeIndex];
    // if (current.list == null || current.list.length == 0) {
    this.loadList();
    // }
  },
})